package com.weic.easycache.framework.biz;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.weic.easycache.framework.Param;
import com.weic.easycache.framework.json.JsonBase;

public abstract class AbsBaseBiz<P extends Param, DATA> implements IApiBiz<DATA>, IDbBiz<DATA>, IImgBiz<DATA> {
	private volatile P param;

	public void setParam(P param) {
		this.param = param;
	}
	
	public P getParam() {
		return param;
	}

	@Override
	public abstract <T> String update(int taskId, CloseableIterator<T> iterator) throws SQLException;

	@Override
	public abstract <T, ID> void createQuery(int taskId, QueryBuilder<T, ID> queryBuilder) throws SQLException;

	@Override
	public <T, ID> String writeDB(int taskId, Dao<T, ID> dao) throws SQLException {
		return RESULT_OK;
	}

	@Override
	public abstract DATA getResult();

	@Override
	public boolean canBgUpdate(int taskId) {
		return false;
	}

	@Override
	public abstract <D> String update(int taskId, JsonBase<D> json, boolean bgTask);

	@Override
	public void fetchParamValues(int taskId, Map<String, String> valueMap) {}

	@Override
	public void updatePageInfo(int taskId, int pageNo, int recordCount, boolean hasMorePage) {}

	@Override
	public <Q> void update(int taskId, String url, Map<String, Q> images) {};

	@Override
	public String[] fetchImgUrls(int taskId) {
		return null;
	}
	
	@Override
	public String getMessage(int taskId) {
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> toList(JsonBase<?> json) {
		ArrayList<T> result = null;
		JsonBase<T> data = (JsonBase<T>)json;
		if(data != null && data.Data != null && data.Data.length > 0) {
			result = new ArrayList<T>();
			Collections.addAll(result, data.Data);
		}
		return result;
	}
}
